﻿
#region Copyright & License
// --------------------------
// Copyright 2008 Daniel Gray
// --------------------------
// This file is part of Kale.
//
// Kale is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Kale is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Kale.  If not, see <http://www.gnu.org/licenses/>.
#endregion
#region File details
// --------------------------------------------------------
// Filename          : Session.cs
// Author            : Daniel Gray
// Created           : 06/05/2008
// --------------------------------------------------------
#endregion
            
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kale.Core.Security;
using System.Collections;
using System.Runtime.Serialization;

namespace Kale.Db.Sql
{
   public partial class Session : ISession
   {
      private bool _isAuthenticated = false;
      private Dictionary<string, object> _sessionObjects;
      private ICollection<KeyValuePair<string, object>> _collection;

      partial void OnCreated()
      {
         _sessionObjects = new Dictionary<string, object>();
         _collection = _sessionObjects as ICollection<KeyValuePair<string, object>>;
      }

      internal bool IsAuthenticated
      {
         get { return _isAuthenticated; }
         set { _isAuthenticated = value; }
      }

      #region ISession Members

      bool ISession.IsAuthenticated
      {
         get { return _isAuthenticated; }
      }

      IUser ISession.Identity
      {
         get 
         {
            return _User.Entity;
         }
      }

      Guid ISession.Id
      {
         get
         {
            return _Uid;
         }
      }

      #endregion

      private static void AssertObjectIsSerializable(object obj)
      {
         if (obj is ISerializable) return;
         if (obj.GetType().GetCustomAttributes(typeof(SerializableAttribute), false).Length > 0) return;
         
         // arrrrrrgggh.
         throw new SerializationException(string.Format("Cannot serialize objects of type {0}", obj.GetType().FullName));
      }

      #region IDictionary<string,object> Members

      void IDictionary<string, object>.Add(string key, object value)
      {
         AssertObjectIsSerializable(value);
         _sessionObjects.Add(key, value);
      }

      bool IDictionary<string, object>.ContainsKey(string key)
      {
         return _sessionObjects.ContainsKey(key);
      }

      ICollection<string> IDictionary<string, object>.Keys
      {
         get 
         {
            return _sessionObjects.Keys;
         }
      }

      bool IDictionary<string, object>.Remove(string key)
      {
         return _sessionObjects.Remove(key);
      }

      bool IDictionary<string, object>.TryGetValue(string key, out object value)
      {
         return _sessionObjects.TryGetValue(key, out value);
      }

      ICollection<object> IDictionary<string, object>.Values
      {
         get 
         {
            return _sessionObjects.Values;
         }
      }

      object IDictionary<string, object>.this[string key]
      {
         get
         {
            return _sessionObjects[key];
         }
         set
         {
            AssertObjectIsSerializable(value);
            _sessionObjects[key] = value;
         }
      }

      #endregion

      #region ICollection<KeyValuePair<string,object>> Members

      void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
      {
         AssertObjectIsSerializable(item.Value);
         _collection.Add(item);
      }

      void ICollection<KeyValuePair<string, object>>.Clear()
      {
         _collection.Clear();
      }

      bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
      {
         return _collection.Contains(item);
      }

      void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
      {
         _collection.CopyTo(array, arrayIndex);
      }

      int ICollection<KeyValuePair<string, object>>.Count
      {
         get 
         {
            return _collection.Count;
         }
      }

      bool ICollection<KeyValuePair<string, object>>.IsReadOnly
      {
         get 
         {
            return _collection.IsReadOnly;
         }
      }

      bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
      {
         return _collection.Remove(item);
      }

      #endregion

      #region IEnumerable<KeyValuePair<string,object>> Members

      IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
      {
         return _collection.GetEnumerator();
      }

      #endregion

      #region IEnumerable Members

      System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
      {
         return _sessionObjects.GetEnumerator() as IEnumerator;
      }

      #endregion
   }
}
